const fs = require('fs')
const path = require('path')
const babelParse = require('@babel/parser');
const traverse = require('@babel/traverse').default;
const { transformFromAst } = require('@babel/core');


function myWebpack(config) {
  return new Compiler(config)
}


class Compiler {
  constructor(options = {}) {
    this.options = options;
  }
  // 启动webpack打包
  run() {
    // 1.读取入口文件
    const filePath = this.options.entry;
    console.log(filePath)
    const file = fs.readFileSync(filePath, 'utf-8')
    // 2.将起解析成ast抽象语法书
    const ast = babelParse.parse(file, {
      sourceType: 'module' ///解析 文件的模块化方案是es module
    });
    // debugger
    console.log(ast)
    //获取文件夹路径
    const dirname = path.dirname(filePath);

    // 定义存储依赖的容器
    const deps = {}
    // 收集依赖
    traverse(ast, {
      // 内部会遍历ast中program.body,判断里面语句类型
      // 如果type：ImportDeclaration 就会触发当前函数

      ImportDeclaration({ node }) {
        // 文件相对路径'./add.js'
        const relativePath = node.source.value;
        // 生成基于入口文件的绝对路径
        const absolutePath = path.resolve(dirname, relativePath);
        // 添加依赖
        deps[relativePath] = absolutePath;
        console.log(node)
      }
    })

    console.log(deps);
    // 编译代码：将代码中浏览器不能识别的代码进行编译
    const {code} = transformFromAst(ast, null, {
      presets: ['@babel/preset-env']
    })
    console.log(code)
  }
}

module.exports = myWebpack